﻿using System.Collections.Generic;
using System.Linq;
using SportsStore.InMemoryRepository.Interfaces;
using SportsStore.Model;

namespace SportsStore.InMemoryRepository.Repositories
{
  public class ProductRepository : IProductRepository
  {
    static readonly IList<Product> Products = new List<Product>
                                {
                                    new Product{ Name = "Football", Price = 25 },
                                    new Product{ Name = "Surf board", Price = 179 },
                                    new Product{ Name = "Running shoes", Price = 95 }
                                };

    public void Dispose()
    {
    }

    public IQueryable<Product> Entities
    {
      get { return Products.AsQueryable(); }
    }

    public Product New()
    {
      return new Product();
    }

    public void Update(Product entity)
    {
      var product = (from s in Products
                  where s.Id == entity.Id
                  select s).First();

      if (product != null)
      {
        product.Name = entity.Name;
        product.Price = entity.Price;
      }
    }

    public void Create(Product entity)
    {
      Products.Add(entity);
    }

    public void Delete(Product entity)
    {
      var product = (from s in Products
                  where s.Id == entity.Id
                  select s).First();

      Products.Remove(product);
    }

    public Product GetProduct(int productId)
    {
      return (from s in Products
              where s.Id == productId
              select s).First();
    }

    public IEnumerable<Product> GetProducts(int pageNumber, int numberOfItems)
    {
      return Products
            .OrderBy(p => p.Id)
            .Skip((pageNumber - 1) * numberOfItems)
            .Take(numberOfItems)
            .ToList();
    }
  }
}
